package com.alex.algorithm.easy;

import java.util.Stack;

public class ImplementQueueUsingStack {

    public static void main(String[] args) {
        UsingStackQueue2 queue = new UsingStackQueue2();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        queue.push(4);
        queue.push(5);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
    }

}

class UsingStackQueue {
    Stack<Integer> integerStack = new Stack<>();
    Stack<Integer> helpStack = new Stack<>();

    public void push(Integer value) {
        integerStack.push(value);
    }

    public synchronized Integer pop() {
        helpStack.clear();
        while (!integerStack.isEmpty()) {
            helpStack.push(integerStack.pop());
        }
        Integer val = helpStack.pop();
        while (!helpStack.isEmpty()) {
            integerStack.push(helpStack.pop());
        }
        return val;
    }

    public Integer peek() {
        helpStack.clear();
        while (!integerStack.isEmpty()) {
            helpStack.push(integerStack.pop());
        }
        Integer val = helpStack.peek();
        while (!helpStack.isEmpty()) {
            integerStack.push(helpStack.pop());
        }
        return val;
    }
}

class UsingStackQueue2 {
    Stack<Integer> integerStack = new Stack<>();
    Stack<Integer> helpStack = new Stack<>();

    public synchronized void push(Integer value) {
        while (!integerStack.isEmpty()) {
            helpStack.push(integerStack.pop());
        }
        helpStack.push(value);
        while (!helpStack.isEmpty()) {
            integerStack.push(helpStack.pop());
        }
    }

    public Integer pop() {
        return integerStack.pop();
    }

    public Integer peek() {
        return integerStack.peek();
    }
}